local skel = fk.CreateSkill {
  name = "rmt__xiuhuaduoming",
  tags = {Skill.Limited},
}

Fk:loadTranslationTable{
  ["rmt__xiuhuaduoming"] = "绣花夺命",
  [":rmt__xiuhuaduoming"] = "限定技，一名角色装备栏均被废除后，你可以复原其装备栏并对其使用其武将牌上的所有“针”，其于结算期间不能使用牌。",

  ["@@rmt__xiuhuaduoming-phase"] = "被绣花夺命",
  ["#rmt__xiuhuaduoming-invoke"] = "绣花夺命：你可以复原 %src 所有装备栏，对其使用其的所有“针”",
  ["#rmt__xiuhuaduoming-use"] = "绣花夺命：选择一张“针”对其使用",

  ["$rmt__xiuhuaduoming1"] = "皇图霸业谈笑中，不胜人生一场醉",
  ["$rmt__xiuhuaduoming2"] = "风刀霜剑何相逼，只叹江湖几人回",
}

skel:addEffect(fk.AreaAborted, {
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(skel.name) and player:usedSkillTimes(skel.name, Player.HistoryGame) == 0 and not target.dead then
      return #target:getAvailableEquipSlots() == 0 and table.find(data.slots, function (slot)
        return slot ~= Player.JudgeSlot
      end)
    end
  end,
  on_cost = function (self, event, target, player, data)
    if player.room:askToSkillInvoke(player, {skill_name = skel.name, prompt = "#rmt__xiuhuaduoming-invoke:"..target.id}) then
      event:setCostData(self, {tos = {target}})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(target, "@@rmt__xiuhuaduoming-phase", 1)
    room.logic:getCurrentEvent():addCleaner(function()
      room:setPlayerMark(target, "@@rmt__xiuhuaduoming-phase", 0)
    end)
    local slots = table.simpleClone(target.equipSlots)
    room:resumePlayerArea(target, slots)
    local pile = table.simpleClone(target:getPile("rmt__sunflowerneedle"))
    while not target.dead do
      local ids = table.filter(pile, function(id)
        if not table.contains(target:getPile("rmt__sunflowerneedle"), id) then return false end
        local card = Fk:getCardById(id)
        return player:canUseTo(card, target, {bypass_distances = true, bypass_times = true})
      end)
      if #ids == 0 then break end
      local cid = ids[1]
      if not player.dead then
        cid = room:askToChooseCard(player, {
          target = target, skill_name = skel.name, prompt = "#rmt__xiuhuaduoming-use",
          flag = { card_data = { { "rmt__sunflowerneedle", ids } } }
        })
      end
      table.removeOne(pile, cid)
      room:useCard{
        from = player,
        tos = {target},
        card = Fk:getCardById(cid),
      }
    end
    -- room:setPlayerMark(target, "@@rmt__xiuhuaduoming-phase", 0)
  end,
})

skel:addEffect("prohibit", {
  prohibit_use = function(self, player, card)
    return player:getMark("@@rmt__xiuhuaduoming-phase") ~= 0 and card
  end,
})

return skel
